package Frame;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.util.Date;
import java.util.Set;
import java.util.Timer;
import java.util.TimerTask;
import java.util.Vector;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JDialog;
import javax.swing.JFileChooser;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.filechooser.FileSystemView;

import Function.FCBlockLibrary;
import VWorldGameStudio.FCBlock;

public class FCEditBlock extends JDialog {
	private JFileChooser filec = new JFileChooser(new File("./material/png/block/")) {
		protected javax.swing.JDialog createDialog(java.awt.Component parent) throws java.awt.HeadlessException {
			JDialog jd = super.createDialog(parent);
			jd.setSize(500, 400);
			jd.setTitle("选择文件");
			jd.setResizable(false);
			jd.setLocationRelativeTo(null);
			return jd;
		};
	};
	private String selectePng;
	private FCBlockLibrary blockl;

	public FCEditBlock(boolean ok, FCBlockLibrary blockl, FCGMaster master) {
		this.blockl = blockl;
		setTitle((ok) ? "方块管理-编辑方块" : "方块管理-新建方块");
		setBounds(0, 0, 775, 625);
		setLayout(null);
		setResizable(false);
		setLocationRelativeTo(null);
		setModal(true);

		JLabel a = new JLabel("方块ID");
		a.setBounds(10, 10, 60, 20);
		this.add(a);
		JComboBox<String> aa = new JComboBox<String>();
		aa.setBounds(70, 10, 130, 20);
		aa.setEnabled(ok);
		JTextField bb = new JTextField();
		bb.setBounds(70, 10, 130, 20);
		if (!ok) {
//			aa.addItem(String.valueOf(blockl.getBlockNewID()));
			this.add(bb);
		} else {
			Set<String> qwe=blockl.getBlockIDList();
			for(String i:qwe)aa.addItem(i);
			this.add(aa);
		}

		JLabel c = new JLabel("方块属性");
		c.setBounds(10, 70, 60, 20);
		this.add(c);
		JComboBox<String> cc = new JComboBox<String>();
		cc.setBounds(70, 70, 130, 20);
		cc.addItem("unplace");
		cc.addItem("place");
		this.add(cc);

		JLabel d = new JLabel("方块材质");
		d.setBounds(10, 100, 60, 20);
		this.add(d);
		JButton dd = new JButton("添加");
		dd.setBounds(130, 100, 70, 20);
		this.add(dd);
		FCJtable ddd = new FCJtable();
		ddd.setBounds(10, 130, 190, 200);
		this.add(ddd);
		dd.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent arg0) {
				if (filec.showOpenDialog(null) == filec.APPROVE_OPTION) {
					ddd.addTexture(filec.getSelectedFile().getPath().replaceAll("\\\\", "/"),
							filec.getSelectedFile().getName());
				}
			}
		});

		JLabel e = new JLabel("绑定事件");
		e.setBounds(10, 340, 60, 20);
		this.add(e);
		JComboBox<String> ee = new JComboBox<String>();
		ee.setBounds(70, 340, 130, 20);
		ee.addItem("null");
		this.add(ee);

		JLabel f = new JLabel("实体坐标");
		f.setBounds(10, 370, 60, 20);
		this.add(f);
		JButton ff = new JButton("选择");
		ff.setBounds(130, 370, 70, 20);
		this.add(ff);
		FCJtable fff = new FCJtable();
		fff.setBounds(10, 400, 190, 160);
		this.add(fff);

		FCMaterailMap map = new FCMaterailMap(ddd, fff);
		map.setBounds(210, 10, 550, 550);
		this.add(map);

		JButton okk = new JButton("确认");
		okk.setBounds(10, 570, 750, 18);
		this.add(okk);
		okk.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				if (bb.getText() != null && !bb.getText().equals("")) {
					if (bb.getText().length() > 4) {
						if (!ok)
							if (!blockl.isItemRepeat(bb.getText())) {
								blockl.registerBlock(!ok,
										bb.getText(), cc.getSelectedItem().toString(), map.getBlockPngUrlArray(!ok),
										map.getBlockModelArray(),
										fff.getBlockCoorArray(map.getBlockFirstAndEndCoorArray()),
										ee.getSelectedItem().toString());
								new FCTip("新建方块-新建方块", "新建成功");
								master.setBlockLibrary(blockl);
								dispose();
							} else
								new FCTip("新建方块-ERROR", "名称重复！");
						else if (blockl.isItemRepeat(bb.getText())) {
							map.setBlockPngUrlVecter();
							map.setFirstAndEndCoor();
							blockl.registerBlock(!ok,aa.getSelectedItem().toString(),
									cc.getSelectedItem().toString(), map.getBlockPngUrlArray(!ok),
									map.getBlockModelArray(), fff.getBlockCoorArray(map.getBlockFirstAndEndCoorArray()),
									ee.getSelectedItem().toString());
							new FCTip("编辑方块-修改方块", "修改成功");
							dispose();
						} else
							new FCTip("编辑方块-ERROR", "该方块不存在！");
					} else
						new FCTip("新建方块-ERROR", "名称长度不得少于5个！");
				} else
					new FCTip("新建方块-ERROR", "名称不得为空！");
			}
		});

		ff.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				map.setEntutyOpenOrClose();
				ddd.setSelect(-1);
				ddd.repaint();
			}
		});

		aa.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent arg0) {
				FCBlock linshi = blockl.getBlockOfID(aa.getSelectedItem().toString());
				bb.setText(linshi.getBlockID());
				cc.setSelectedItem(linshi.getBlockType());
				String[] qwe = linshi.getBlockPngUrl();
				ddd.clearTexture();
				for (int i = 0; i < qwe.length; i++) {
					ddd.addTexture(qwe[i], qwe[i]);
				}
				map.loadSelectBlockData(linshi.getBlockPngUrl(), linshi.getBlockModel());
				int[][] asd = linshi.getBlockCoor();
				fff.clearTexture();
				for (int i = 0; i < asd.length; i++) {
					for (int j = 0; j < asd[0].length; j++) {
						if (asd[i][j] > -1) {
							fff.addTexture(i + "," + j + "=" + asd[i][j]);
						}
					}
				}
			}
		});

		Timer ti = new Timer();
		ti.schedule(new updateUI(map), new Date(), 50);

		setVisible(true);
	}

	private class updateUI extends TimerTask {
		private FCMaterailMap a;

		public updateUI(FCMaterailMap a) {
			this.a = a;
		}

		@Override
		public void run() {
			a.repaint();
		}
	}
}
